Skip to content

Support to autoconfigure multiple chat models at same time #3172

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

quaff
Copy link
Contributor

@quaff quaff commented May 15, 2025

Given:

implementation("org.springframework.ai:spring-ai-starter-model-deepseek")
implementation("org.springframework.ai:spring-ai-starter-model-ollama")

Application failed to start before this commit:

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 1 of method chatClientBuilder in org.springframework.ai.model.chat.client.autoconfigure.ChatClientAutoConfiguration required a single bean, but 2 were found:
	- deepSeekChatModel: defined by method 'deepSeekChatModel' in class path resource [org/springframework/ai/model/deepseek/autoconfigure/DeepSeekChatAutoConfiguration.class]
	- ollamaChatModel: defined by method 'ollamaChatModel' in class path resource [org/springframework/ai/model/ollama/autoconfigure/OllamaChatAutoConfiguration.class]

After this commit, we could construct our own instead of autoconfigured ChatClient.Builder by injecting qualified ChatModel.

Related to GH-2221.

Given:
```
implementation("org.springframework.ai:spring-ai-starter-model-deepseek")
implementation("org.springframework.ai:spring-ai-starter-model-ollama")

```

Application failed to start before this commit:

```
***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 1 of method chatClientBuilder in org.springframework.ai.model.chat.client.autoconfigure.ChatClientAutoConfiguration required a single bean, but 2 were found:
	- deepSeekChatModel: defined by method 'deepSeekChatModel' in class path resource [org/springframework/ai/model/deepseek/autoconfigure/DeepSeekChatAutoConfiguration.class]
	- ollamaChatModel: defined by method 'ollamaChatModel' in class path resource [org/springframework/ai/model/ollama/autoconfigure/OllamaChatAutoConfiguration.class]

```

After this commit, we could construct our own instead of autoconfigured `ChatClient.Builder` by injecting qualified `ChatModel`.

Related to spring-projectsGH-2221.

Signed-off-by: Yanming Zhou <zhouyanming@gmail.com>
@markpollack
Copy link
Member

Hi. How does this PR work to achieve the goal of injecting by qualified chat model? Holding off on this until the GA release.

@markpollack markpollack added this to the 1.0.x milestone May 15, 2025
@quaff
Copy link
Contributor Author

quaff commented May 16, 2025

Hi. How does this PR work to achieve the goal of injecting by qualified chat model? Holding off on this until the GA release.

Application could define their own ChatClient.Builders like this:

	@Bean
	@Scope("prototype")
	ChatClient.Builder deepSeekChatClientBuilder(ChatClientBuilderConfigurer chatClientBuilderConfigurer, @Qualifier("deepSeekChatModel") ChatModel deepSeekChatModel,
										 ObjectProvider<ObservationRegistry> observationRegistry,
										 ObjectProvider<ChatClientObservationConvention> observationConvention) {
		ChatClient.Builder builder = ChatClient.builder(deepSeekChatModel,
				observationRegistry.getIfUnique(() -> ObservationRegistry.NOOP),
				observationConvention.getIfUnique(() -> null));
		return chatClientBuilderConfigurer.configure(builder);
	}

	@Bean
	@Scope("prototype")
	ChatClient.Builder ollamaChatClientBuilder(ChatClientBuilderConfigurer chatClientBuilderConfigurer, @Qualifier("ollamaChatModel") ChatModel ollamaChatModel,
												 ObjectProvider<ObservationRegistry> observationRegistry,
												 ObjectProvider<ChatClientObservationConvention> observationConvention) {
		ChatClient.Builder builder = ChatClient.builder(ollamaChatModel,
				observationRegistry.getIfUnique(() -> ObservationRegistry.NOOP),
				observationConvention.getIfUnique(() -> null));
		return chatClientBuilderConfigurer.configure(builder);
	}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants